<template>
  <div>
    <el-button type="primary" @click="openAddDialog(null)">添加客户</el-button>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="60">
      </el-table-column>
      <el-table-column
          prop="customerAvatar"
          label="头像"
          width="120">
        <template slot-scope="scope">
          <img :src="scope.row.customerAvatar" alt="头像" style="width: 50px; height: 50px;">
        </template>
      </el-table-column>
      <el-table-column
          prop="customerName"
          label="姓名"
          width="120">
      </el-table-column>
      <el-table-column
          prop="birthDate"
          label="出生日期"
          width="120">
      </el-table-column>
      <el-table-column
          prop="gender"
          label="性别"
          width="60">
      </el-table-column>
      <el-table-column
          prop="provinceId"
          label="省份"
          width="120">
      </el-table-column>
      <el-table-column
          prop="cityId"
          label="城市"
          width="120">
      </el-table-column>
      <el-table-column
          prop="districtId"
          label="区/县"
          width="120">
      </el-table-column>
      <el-table-column
          label="登录密码"
          width="120">
        <template scope="">
          ********
        </template>
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱"
          width="180">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="180">
      </el-table-column>
      <el-table-column
          prop="createdBy"
          label="创建人"
          width="120">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间"
          width="180">
      </el-table-column>
      <el-table-column
          prop="updatedBy"
          label="更新人"
          width="120">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="备注"
          width="180">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small" @click="openAddDialog(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑客户的 Dialog -->
    <el-dialog :title="title" :visible.sync="addDialogVisible">
      <el-form ref="addForm" :model="addFormData">
        <el-form-item label="头像" prop="customerAvatar">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:10001/customer/customerUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="customerName">
          <el-input v-model="addFormData.customerName"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker v-model="addFormData.birthDate" type="date"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group vngModel="addFormData.gender">
            <el-radio label="Male">男</el-radio>
            <el-radio label="Female">女</el-radio>
            <el-radio label="Other">其他</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="省份" prop="provinceId">
          <el-select v-model="addFormData.provinceId">
            <!-- 这里可以动态加载省份选项 -->
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select v-model="addFormData.cityId">
            <!-- 这里可以动态加载城市选项 -->
          </el-select>
        </el-form-item>
        <el-form-item label="区/县" prop="districtId">
          <el-select v-model="addFormData.districtId">
            <!-- 这里可以动态加载区县选项 -->
          </el-select>
        </el-form-item>
        <el-form-item label="登录密码" prop="loginPassword">
          <el-input v-model="addFormData.loginPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addFormData.email"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addFormData.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitData">{{ buttonText }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CustomerView",
  data() {
    return {
      tableData: [],
      title: '',
      addDialogVisible: false,
      addFormData: {
        customerAvatar: '',
        customerName: '',
        birthDate: null,
        gender: '',
        provinceId: '',
        cityId: '',
        districtId: '',
        loginPassword: '',
        email: '',
        remark: ''
      },
      isEdit: false,
      imageUrl: '',
      buttonText: '确定'
    }
  },
  methods: {
    updataCustomer(row) {
      this.axios.post("/customer/CustomerUpdata", row).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
        this.fetchData();
      });
    },
    handleClick(row) {
      console.log("查看操作", row);
      // 可以在这里添加查看详情的逻辑
    },
    fetchData() {
      // 模拟从后端获取数据
      this.axios.get("/customer/CustomerList").then(res => {
        this.tableData = res.data.data;
      });
    },
    openAddDialog(row) {
      if (row) {
        this.isEdit = true;
        this.title = "编辑信息";
        this.buttonText = '保存修改';
        this.addFormData = {...row };
      } else {
        this.isEdit = false;
        this.title = "添加客户";
        this.buttonText = '确定';
        this.addFormData = {
          customerAvatar: '',
          customerName: '',
          birthDate: null,
          gender: '',
          provinceId: '',
          cityId: '',
          districtId: '',
          loginPassword: '',
          email: '',
          remark: ''
        };
      }
      this.addDialogVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.addFormData.customerAvatar = res.data
      console.log(res.data)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitData() {
      if (this.isEdit) {
        this.updataCustomer(this.addFormData);
      } else {
        this.addCustomer();
      }
      this.addDialogVisible = false;
    },
    addCustomer() {
      // 发送添加客户的请求到后端
      this.axios.post('/customer/CustomerInsert', this.addFormData).then(() => {
        this.fetchData();
      });
    }
  },
  created() {
    this.fetchData();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
